package actions.generate;

public class BindViewBeans {
    public static class BindModel {
        private String type;
        private final String id;
        private String propertyName;

        public BindModel(String type, String id) {
            this.type = type;
            this.id = id;
            formatType();
            formatIdToName();
        }

        private void formatType() {
            if (type == null || type.isEmpty()) {
                return;
            }
            String[] parts = type.split("\\.");
            if (parts.length > 0) {
                type = parts[parts.length - 1];
            }
        }

        /**
         * 格式化id，生成默认的 property Name 属性名
         *
         * 移除 id 中的下划线，并将后续的第一个小写英文字母变大写（如果是英文小写，如果不是小写英文则不变更）
         * 测试用例{@link BindModel#main(String[])}
         */
        private void formatIdToName() {
            StringBuilder pro = new StringBuilder();
            int p = 0, len = id.length(), offset = 'A' - 'a';
            boolean needToUpper = false;
            while (p < len) {
                char c = id.charAt(p++);
                if (c == '_') {
                    needToUpper = true;
                } else {
                    if (needToUpper) {
                        if (c >= 'a' && c <= 'z') {
                            c = (char) (c + offset);
                        }
                    }
                    pro.append(c);
                    needToUpper = false;
                }
            }
            propertyName = pro.toString();
        }

        /**
         * {@link BindModel#formatIdToName()} 测试用例
         */
        public static void main(String[] args) {
            System.out.println(new BindModel("aa", "main_tv").propertyName);;
            System.out.println(new BindModel("aa", "maintv").propertyName);;
            System.out.println(new BindModel("aa", "mainTv").propertyName);;
            System.out.println(new BindModel("aa", "main_tv_1s_ss1").propertyName);;
        }

        public void setPropertyName(String propertyName) {
            this.propertyName = propertyName;
        }

        public String getType() {
            return type;
        }

        public String getId() {
            return id;
        }

        public String getPropertyName() {
            return propertyName;
        }
    }
}
